import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class MathProblemViewer extends JFrame {

    public MathProblemViewer() {
        initializeGUI();
        loadAndDisplayProblems();
    }

    private void initializeGUI() {
        setTitle("口算习题查看器");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 400);
        setLocationRelativeTo(null); // 窗口居中显示

        // 设置字体
        Font titleFont = new Font("微软雅黑", Font.BOLD, 18);
        Font problemFont = new Font("宋体", Font.PLAIN, 16);
    }

    private void loadAndDisplayProblems() {
        try {
            // 读取CSV文件
            BufferedReader reader = new BufferedReader(new FileReader("math_problems.csv"));
            String line;
            StringBuilder content = new StringBuilder();
            int problemCount = 0;

            // 读取文件内容
            boolean firstLine = true;
            while ((line = reader.readLine()) != null) {
                if (firstLine) {
                    firstLine = false;
                    continue; // 跳过标题行
                }

                String[] parts = line.split(",");
                if (parts.length >= 2) {
                    problemCount++;
                    String problem = parts[0].trim();
                    String answer = parts[1].trim();
                    content.append(String.format("%2d. %s %s\n", problemCount, problem, answer));
                }
            }
            reader.close();

            // 创建显示区域
            JTextArea textArea = new JTextArea(content.toString());
            textArea.setFont(new Font("宋体", Font.PLAIN, 16));
            textArea.setEditable(false); // 设置为只读

            // 添加滚动条
            JScrollPane scrollPane = new JScrollPane(textArea);
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

            // 创建标题标签
            JLabel titleLabel = new JLabel("100以内加减法口算习题（共" + problemCount + "道）", JLabel.CENTER);
            titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
            titleLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

            // 设置布局
            setLayout(new BorderLayout());
            add(titleLabel, BorderLayout.NORTH);
            add(scrollPane, BorderLayout.CENTER);

            System.out.println("成功加载 " + problemCount + " 道习题");

        } catch (IOException e) {
            // 如果文件不存在，显示错误信息
            JLabel errorLabel = new JLabel(
                    "<html><center>无法找到习题文件！<br>请先运行 MathProblemGenerator 生成 math_problems.csv 文件</center></html>",
                    JLabel.CENTER
            );
            errorLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
            errorLabel.setForeground(Color.RED);
            add(errorLabel, BorderLayout.CENTER);

            System.err.println("读取文件失败: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        // 设置GUI外观为系统默认样式
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 在事件分发线程中创建和显示GUI
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MathProblemViewer().setVisible(true);
            }
        });
    }
}